---@diagnostic disable: lowercase-global, undefined-global, duplicate-set-field, assign-type-mismatch
mapPath = arg[1]
excelId = arg[2]

function AreAllElementsSame(tbl)
    if #tbl == 0 then
        return true, nil -- 空表视为所有元素相同，但没有共同值
    end
    local firstElement = tbl[1]
    for _, element in ipairs(tbl) do
        if element ~= firstElement then
            return false
        end
    end
    return true, firstElement -- 所有元素相同，返回true和共同值
end

function SetSingleValueIfAllSame(obj, keyToCheck)
    local isSame, commonValue = AreAllElementsSame(obj[keyToCheck])
    if isSame then
        -- 如果所有元素相同，则将该key的值设置为共同值
        obj[keyToCheck] = commonValue
    end
end

function string.clear_color(str)
    str = str:gsub('(|[cC]%w%w%w%w%w%w%w%w)(.-)|[rR]', function(color, str)
        return str
    end)
    return str
end

local lni_loader = require 'lni-loader_lua'

Xlsx.RealWPSId(excelId, function(workbook)
    local config_sheet = workbook:GetSheet '配置'
    if not config_sheet then
        print '不存在配置工作表'
        return
    end
    -- 打开配置工作表
    local config = {}
    local sheetName = {}
    local pageData = {}
    for row = config_sheet.FirstRowNum, config_sheet.LastRowNum do
        local rowData = config_sheet:GetRow(row)
        local sheet_cell = rowData:GetCell(0)
        local data_cell = rowData:GetCell(1)
        local path_cell = rowData:GetCell(2)

        --有值，导出此表数据
        if sheet_cell then
            if workbook:HasSheet(sheet_cell.StringCellValue) then
                if config[data_cell.StringCellValue] == nil then
                    config[data_cell.StringCellValue] = { path = path_cell.StringCellValue }
                end
                table.insert(config[data_cell.StringCellValue], sheet_cell.StringCellValue)
                table.insert(sheetName, sheet_cell.StringCellValue)
            else
                print('不存在此工作表', sheet_cell.StringCellValue)
            end
        end
    end

    local types = {
        [''] = 'nil',
        ['index'] = 'index',
    }

    --读取所有Excel数据
    for _, name in ipairs(sheetName) do
        local sheet = workbook:GetSheet(name)

        --查看工作表读取类型方式
        if sheet then
            local type = sheet:GetCell(0, 0).StringCellValue
            xpcall(function(...)
                local lists = require 'book_type' (types[type])(sheet)
                pageData[name] = lists
            end, function(msg)
                print('error:', msg)
            end)
        end
    end
    local pool = {}
    for key, key_data in pairs(config) do
        if not pool[key] then
            pool[key] = {}
        end

        for _, name in ipairs(key_data) do
            local datas = pageData[name]
            for index, tbl in ipairs(datas) do
                local name = tbl.key
                local object = tbl.value

                --已经有同名表了
                if pool[key][name] then
                    if pool[key][name].is_list == nil then
                        pool[key][name] = { is_list = true, pool[key][name] }
                    end
                    table.insert(pool[key][name], object)
                else
                    pool[key][name] = object
                end
            end
        end
    end

    for t, data in pairs(config) do
        xpcall(function(...)
            local datas = pool[t]
            local new_datas = {}
            for key, object in pairs(datas) do
                if type(key) == 'string' then
                    key = key:clear_color()
                end
                new_datas[key] = object
            end
            pool[t] = new_datas
            for key, object in pairs(new_datas) do
                --是一个列表数据
                if object.is_list then
                    --查看有没有level属性，有应该是一个技能，再次转化
                    local function check_level()
                        local b = true
                        for k, v in ipairs(object) do
                            if v.level == nil then
                                b = false
                                break
                            end
                        end
                        return b
                    end
                    if check_level() then
                        local new_object = { level = 1, max_level = #object }
                        for i, obj in ipairs(object) do
                            for k, v in pairs(obj) do
                                if k ~= 'level' then
                                    if new_object[k] == nil then
                                        new_object[k] = {}
                                    end
                                    new_object[k][#new_object[k] + 1] = v
                                end
                            end
                        end
                        for k, v in pairs(new_object) do
                            if type(v) == 'table' then
                                SetSingleValueIfAllSame(new_object, k)
                            end
                        end
                        new_datas[key] = new_object
                    end
                end
                object.is_list = nil
            end
            lni_loader:save_data(new_datas, mapPath .. data['path'])
        end, function(msg)
            print(msg)
        end)
    end
end):Wait()
